<?xml version="1.0"?>

<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:exsl="http://exslt.org/common">

	<xsl:variable name="Data">
		<xsl:text>Data</xsl:text>
	</xsl:variable>

	<xsl:variable name="Object">
		<xsl:text>Object</xsl:text>
	</xsl:variable>

	<xsl:variable name="Collection">
		<xsl:text>Collection</xsl:text>
	</xsl:variable>

	<xsl:template name="CollectionVariableSQLTableName">
		<xsl:param name="Class" />
		<xsl:param name="CollectionType" />

		<xsl:value-of select="$Class/@name"/>
		<xsl:text>__</xsl:text>
		<xsl:value-of select="$CollectionType/@variable"/>
		<xsl:text>__Collection</xsl:text>
	</xsl:template>

	<xsl:template name="GetType">
		<xsl:param name="Type" />
		<xsl:choose>
			<xsl:when test="name()='String'">
				<xsl:text>getString</xsl:text>
			</xsl:when>
			<xsl:when test="name()='Integer'">
				<xsl:text>getInt</xsl:text>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="JavaTypeFromElement">
		<xsl:param name="Element" />
		<xsl:choose>
			<xsl:when test="name($Element)='Integer'">
				<xsl:text>Integer</xsl:text>
			</xsl:when>
			<xsl:when test="name($Element)='String'">
				<xsl:text>String</xsl:text>
			</xsl:when>
			<xsl:when test="name($Element)='Object'">
				<xsl:value-of select="@class" />
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<!-- Returns the added columns after drops and renames have been taken into account -->
	<!-- Not working yet -->
	<xsl:template name="UpToDateColumns">
		<xsl:param name="Class" />

		<xsl:copy-of select="$Class/Build"/>
	</xsl:template>

	<xsl:template name="SelectAllDataAndObjects">
		<xsl:param name="Class" />
		<xsl:copy-of select="$Class/Build/Add/Integer | $Class/Build/Add/String | $Class/Build/Add/Object"/>
	</xsl:template>

	<xsl:template name="SelectAllCollections">
		<xsl:param name="Class" />
		<xsl:copy-of select="$Class/Build/Add/List | $Class/Build/Add/Array"/>
	</xsl:template>

	<xsl:template name="SelectAllCollectionElements">
		<xsl:param name="Class" />
		<xsl:copy-of select="$Class/Build/Add/Array/* | $Class/Build/Add/List/*"/>
	</xsl:template>

	<xsl:template name="IsNullable">
		<xsl:param name="Element" />
		
		<xsl:choose>
			<xsl:when test="name($Element)='Integer'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='String'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='Object'">
				<xsl:number value="1" />
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="ElementType">
		<xsl:param name="Element" />
		
		<xsl:choose>
			<xsl:when test="name($Element)='Integer'">
				<xsl:value-of select="$Data"/>
			</xsl:when>
			<xsl:when test="name($Element)='String'">
				<xsl:value-of select="$Data"/>
			</xsl:when>
			<xsl:when test="name($Element)='Object'">
				<xsl:value-of select="$Object"/>
			</xsl:when>
			<xsl:when test="name($Element)='Array'">
				<xsl:value-of select="$Collection"/>
			</xsl:when>
			<xsl:when test="name($Element)='List'">
				<xsl:value-of select="$Collection"/>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="GotListAdd">
		<xsl:param name="Element" />
		<xsl:param name="JavaType" />
		<xsl:choose>
			<xsl:when test="name($Element)='Integer'">
								gotList.add(listC.getInt(_childIndex2));
			</xsl:when>
			<xsl:when test="name($Element)='String'">
								gotList.add(listC.getString(_childIndex2));
			</xsl:when>
			<xsl:when test="name($Element)='Object'">
								/*This is just to get the XSL to play nice with the formatting*/
								<xsl:value-of select="$JavaType" /> newValue2 = new <xsl:value-of select="$JavaType" />();
								newValue2 = DatabaseHelper.<xsl:value-of select="$JavaType" />Helper.Query.GetById(db, listC.getLong(_childIndex2));
								gotList.add(newValue2);
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="SetListAdd">
		<xsl:param name="CollectionType" />
		<xsl:param name="JavaType" />
		<xsl:choose>
			<xsl:when test="name($CollectionType)='List'">
							object.<xsl:value-of select="MethodMap/@set" />(gotList);
			</xsl:when>
			<xsl:when test="name($CollectionType)='Array'">
							object.<xsl:value-of select="MethodMap/@set" />(gotList.toArray(new <xsl:value-of select="$JavaType" />[0]));
			</xsl:when>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
